
package com.cwf.business.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.cwf.business.util.AppConstant;
import com.cwf.business.util.DFStatusBar;
import com.cwf.business.util.DFToolbar;
import com.cwf.business.util.MainUtil;

/**
 * Created by sam_chan on 2017/5/8.
 */
public abstract class DFActivity extends AppCompatActivity {


    private boolean isOfflineActivity;      // 用于标志当前页面是否是离线也可以进入的页面（比如FE图片浏览页面或者FE文档浏览页面）

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupStatusBar();
        setupHomeWatcherDog();
    }

    private void setupHomeWatcherDog() {
        IntentFilter filter = new IntentFilter(AppConstant.EXIT_APPLICATION);
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        registerReceiver(mExitReceiver, filter);
    }

    private void setupStatusBar() {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            optionStatusBar();
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (DFStatusBar.canModifyStatusBar(this.getWindow())) {
                WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
                optionStatusBar();
                return;
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                return;
            }

            getWindow().setStatusBarColor(statusBarColor());
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
    }

    protected void toolBar(DFToolbar toolbar) {
    }

    protected int statusBarColor() {
        return Color.parseColor("#55000000");
    }

    /**
     * 不需要处理状态栏，please return false.
     * 需要对状态栏进行处理：return DFStatusBar.setDarkStatusBar() or DFStatusBar.setLightStatusBar();
     */
    protected boolean optionStatusBar() {
        return DFStatusBar.setDarkStatusBar(this);
    }

    @Override
    public void startActivity(Intent intent) {
        MainUtil.closeKeyBoardWhenNeed(this);
        super.startActivity(intent);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        MainUtil.closeKeyBoardWhenNeed(this);
        super.startActivityForResult(intent, requestCode, options);
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void setContentView(int layoutResID) {
        this.setContentView(layoutResID, false);
    }

    @Override
    public void setContentView(View view) {
        this.setContentView(view, false);
    }

    public void setContentView(View view, boolean isOfflineActivity) {
        super.setContentView(view);
        this.isOfflineActivity = isOfflineActivity;
    }

    public void setContentView(int layoutResID, boolean isOfflineActivity) {
        super.setContentView(layoutResID);
        this.isOfflineActivity = isOfflineActivity;
    }

    protected void onSwipeOpened() { }

    protected boolean isAbleToSwipe() {
        return true;
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onRestoreInstanceState(Bundle state) { // 处理改变语言时出现的崩溃现象
        if (state != null) {
            try {
                super.onRestoreInstanceState(state);
            } catch (final Exception ignored) {

            }
            state = null;
        }
    }

    public void exitApplication(boolean isNeedLoginOut) {   // 关闭所有Activity 并退出应用程序
        final Intent exitIntent = new Intent(AppConstant.EXIT_APPLICATION);
        exitIntent.putExtra(MainActivity.LOGOUT_KEY_NAME, isNeedLoginOut);
        sendBroadcast(exitIntent);
    }

    public void restartApplication() {
        startActivity(new Intent(this, SplashActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mExitReceiver);
        System.gc();
    }

    public void findView() { }

    public void bindData() { }

    public void setListener() { }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getIntent().getBooleanExtra("isNotification", false)) {         // 处理由消息推送进入的界面返回逻辑
            final Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }

    private final BroadcastReceiver mExitReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_LOCALE_CHANGED.equals(action) || AppConstant.EXIT_APPLICATION.equals(action)) {// 换语言
                finish();
            }
        }
    };
}
